package cn.drenal.capcap.common;

import lombok.Data;

/**
 * 统一响应体
 * @param <T> 响应数据类型
 */
@Data
public class Response<T> {
    /**
     * 状态码
     */
    private int code;

    /**
     * 响应消息
     */
    private String message;

    /**
     * 响应数据
     */
    private T data;

    /**
     * 成功响应
     */
    public static <T> Response<T> success() {
        return success(null);
    }

    /**
     * 成功响应（带数据）
     */
    public static <T> Response<T> success(T data) {
        Response<T> response = new Response<>();
        response.setCode(ResponseCode.SUCCESS.getCode());
        response.setMessage(ResponseCode.SUCCESS.getMessage());
        response.setData(data);
        return response;
    }

    /**
     * 失败响应
     */
    public static <T> Response<T> error(ResponseCode responseCode) {
        return error(responseCode, null);
    }

    /**
     * 失败响应（带数据）
     */
    public static <T> Response<T> error(ResponseCode responseCode, T data) {
        Response<T> response = new Response<>();
        response.setCode(responseCode.getCode());
        response.setMessage(responseCode.getMessage());
        response.setData(data);
        return response;
    }

    /**
     * 失败响应（仅消息）
     */
    public static <T> Response<T> error(String message) {
        Response<T> response = new Response<>();
        response.setCode(ResponseCode.INTERNAL_SERVER_ERROR.getCode());
        response.setMessage(message);
        response.setData(null);
        return response;
    }

    /**
     * 自定义响应
     */
    public static <T> Response<T> custom(int code, String message, T data) {
        Response<T> response = new Response<>();
        response.setCode(code);
        response.setMessage(message);
        response.setData(data);
        return response;
    }
}